package com.thinkgem.jeesite.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.kpoint.core.base.KpointBaseEntity;
import com.thinkgem.jeesite.common.utils.Collections3;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 6:39 2018/8/15
 * @Modified By:
 */
@Data
public class SysUser extends KpointBaseEntity {
    public static final String COL_OFFICE_ID = "`office_id`";
    public static final String COL_LOGIN_NAME = "`login_name`";
    public static final String COL_PASSWORD = "`password`";
    public static final String COL_NO = "`no`";
    public static final String COL_NAME = "`name`";
    public static final String COL_EMAIL = "`email`";
    public static final String COL_PHONE = "`phone`";
    public static final String COL_MOBILE = "`mobile`";
    public static final String COL_USER_TYPE = "`user_type`";
    public static final String COL_PHOTO = "`photo`";
    public static final String COL_LOGIN_IP = "`login_ip`";
    public static final String COL_LOGIN_DATE = "`login_date`";
    public static final String COL_LOGIN_FLAG = "`login_flag`";


    private static final long serialVersionUID = 1L;

    /**
     * 公司
     */
    private String companyId;
    /**
     * 机构
     */
    private String officeId;
    /**
     * 登录名
     */
    private String loginName;
    /**
     * 密码
     */
    private String password;

    /**
     * 名称
     */
    private String no;
    /**
     * 名称
     */
    private String name;
    /**
     * 电子邮箱
     */
    private String email;
    /**
     * 座机号
     */
    private String phone;
    /**
     * 手机号
     */
    private String mobile;
    /**
     * 用户类型
     */
    private String userType;
    /**
     * 照片
     */
    private String photo;
    /**
     * 登录Ip
     */
    private String loginIp;
    /**
     * 登录时间
     */
    private Date loginDate;
    /**
     * 是否允许登录
     */
    private String loginFlag;

    @TableField(exist = false)
    private SysOffice company;

    @TableField(exist = false)
    private SysOffice office;
    /**
     * 新密码
     */
    @TableField(exist = false)
    private String newPassword;
    /**
     * 上次登录地点
     */
    @TableField(exist = false)
    private String oldLoginIp;
    /**
     * 上次登录时间
     */
    @TableField(exist = false)
    private Date oldLoginDate;
    /**
     * 原登录名
     */
    @TableField(exist = false)
    private String oldLoginName;
    /**
     * 拥有角色列表
     */
    @TableField(exist = false)
    private List<SysRole> roleList = Lists.newArrayList();

    public static boolean isAdmin(String id) {
        return id != null && "1".equals(id);
    }

    @JsonIgnore
    public List<String> getRoleIdList() {
        List<String> roleIdList = Lists.newArrayList();
        for (SysRole role : roleList) {
            roleIdList.add(role.getId());
        }
        return roleIdList;
    }

    public void setRoleIdList(List<String> roleIdList) {
        roleList = Lists.newArrayList();
        for (String roleId : roleIdList) {
            SysRole role = new SysRole();
            role.setId(roleId);
            roleList.add(role);
        }
    }

    /**
     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
     */
    public String getRoleNames() {
        return Collections3.extractToString(roleList, "name", ",");
    }

    public boolean isAdmin() {
        return isAdmin(this.getId());
    }



}
